home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_4 / issue_02 / help / curvefit / manual < prev   
Encoding:
Text File  |  1990-10-21  |  5.8 KB  |  202 lines

  1. LSLinF - Least Squares Linear Function program
  2. ==============================================
  3.  
  4. What it does :-
  5. ---------------
  6.  
  7. given data points (x,y) or (x(1),x(2),y) etc. produce a curve fit of the form
  8.  
  9. y = a(1)*f1(x(1)) + a(2)*f2(x(1)) + ...
  10.  
  11. or
  12.  
  13. y = a(1)*f1(x(1),x(2),...) + a(2)*f2(x(1),x(2),...) + ...
  14.  
  15. and find the "best" values of a(1), a(2), etc. to minimise the error.
  16.  
  17. The functions f1, f2 etc. can be any of the standard BASIC functions or
  18. user-defined functions in BASIC.
  19.  
  20. Example 1. A simple starter
  21. ---------------------------
  22.  
  23. Suppose your data looks like this
  24.  
  25.    x    y
  26.  
  27.    1    0.0
  28.    1.4 -1.2
  29.    2   -0.3
  30.    4    1.4
  31.    5.7  3.75
  32.  
  33. and you would like a curve fit of the form
  34.  
  35. y = a1 + a2*x + a3*tan(x/10) + a4*exp(x/10)
  36.  
  37. the lines to enter in the program are
  38.  
  39.    30DATA 4             : REM NUMBER OF FUNCTION TERMS
  40.    40DATA 1,X(1),TAN(X(1)/10),EXP(X(1)/10) :REM FUNCTION TERMS
  41.    50DATA 1             : REM NO. OF INDEPENDENT VARIABLES
  42.    60DATA X             : REM INDEPENDENT VARIABLE TRANSFORMATION FUNCTION(S)
  43.    70DATA Y             : REM   DEPENDENT VARIABLE TRANSFORMATION FUNCTION
  44.  
  45. and add your data at the end at say line 10000
  46.  
  47. 10000 DATA   1  ,  0.0
  48. 10010 DATA   1.4, -1.2
  49. 10020 DATA   2  , -0.3
  50. 10030 DATA   4  ,  1.4
  51. 10040 DATA   5.7,  3.75
  52.  
  53. The output looks like this
  54.  
  55. LEAST SQUARE LINEAR FUNCTION FIT
  56.  Y = A1*(1) + A2*(X(1)) + A3*(TAN(X(1)/10)) + A4*(EXP(X(1)/10))
  57.  
  58. A1 = 138.800998
  59. A2 = -6.61692633
  60. A3 = 244.268219
  61. A4 = -143.588419
  62.  
  63. RMS error = 3.35249813E-3
  64. MAX error = 5.95496874E-3
  65.  
  66. Press <RETURN> to display a graph of the function
  67.  
  68. Example 2. This shows the use of 2 independent variables
  69. --------------------------------------------------------
  70.  
  71. Suppose your data looks like this:
  72.  
  73.      x(1)     x(2)     y
  74.  
  75.      1.0      0.0      1.0
  76.      1.0      1.0      4.0
  77.      1.0      2.0      7.0
  78.      2.0      1.0      7.0
  79.      2.0      2.0     10.0
  80.      2.0      3.0     13.0
  81.  
  82. This data actually represents the equation y = x(1)^2 + 3*x(2), so we can
  83. check if the program finds the right surface-fit. Your input data could
  84. look like this:
  85.  
  86.    30DATA 5             : REM NUMBER OF FUNCTION TERMS
  87.    40DATA 1,X(1),X(1)^2,X(2),X(2)^2   : REM FUNCTION TERMS
  88.    50DATA 2             : REM NO. OF INDEPENDENT VARIABLES
  89.    60DATA X,X           : REM INDEPENDENT VARIABLE TRANSFORMATION FUNCTION(S)
  90.    70DATA Y             : REM   DEPENDENT VARIABLE TRANSFORMATION FUNCTION
  91.  
  92. We are specifying more function terms than are needed, since we already
  93. know the answer, but this will be a good test for the program.
  94. Add your data at the end at say line 10000
  95.  
  96. 10000 DATA 1,0,1
  97. 10010 DATA 1,1,4
  98. 10020 DATA 1,2,7
  99. 10030 DATA 2,1,7
  100. 10040 DATA 2,2,10
  101. 10050 DATA 2,3,13
  102. 10060 DATA 3,0,9
  103.  
  104. And your output should look like this:
  105.  
  106. LEAST SQUARE LINEAR FUNCTION FIT
  107.  Y = A1*(1) + A2*(X(1)) + A3*(X(1)^2) + A4*(X(2)) + A5*(X(2)^2)
  108.  
  109. A1 = 1.27724239E-7
  110. A2 = -2.02096998E-7
  111. A3 = 1.00000005
  112. A4 = 3.00000005
  113. A5 = -1.11758709E-8
  114.  
  115. RMS error = 1.92788031E-8
  116. MAX error = 3.35276127E-8
  117.  
  118. Notice that the terms in 1, X(1) and X(2)^2 have coefficients approximately
  119. zero, since they are not needed. Notice also, that A3 and A4 are as
  120. expected.
  121.  
  122.  
  123. Example3. Transformation of x(1)
  124. --------------------------------
  125.  
  126. Transformation modifies the value of x input points before curve-fitting
  127. takes place. For example, if you have very large values, you could
  128. divide them all by 1000 to avoid overflow, or you could, say, take the
  129. log of your data if you think the curve-fit might work better like this.
  130. Here is an example of some data which benefits from a transformation
  131.  
  132.    30DATA 3             : REM NUMBER OF FUNCTION TERMS
  133.    40DATA X(1),X(1)^2,X(1)^3
  134.    50DATA 1             : REM NO. OF INDEPENDENT VARIABLES
  135.    60DATA SQR(X)        : REM INDEPENDENT VARIABLE TRANSFORMATION FUNCTION(S)
  136.    70DATA Y             : REM   DEPENDENT VARIABLE TRANSFORMATION FUNCTION
  137.  
  138. x is transformed to SQR(x).
  139.  
  140. 10000DATA 1.000000,0.000000
  141. 10010DATA 0.975528,0.004834
  142. 10020DATA 0.904508,0.018164
  143. 10030DATA 0.793893,0.036729
  144. 10040DATA 0.654508,0.055902
  145. 10050DATA 0.500000,0.070711
  146. 10060DATA 0.345492,0.076942
  147. 10070DATA 0.206107,0.072084
  148. 10080DATA 0.095492,0.055902
  149. 10090DATA 0.024472,0.030521
  150. 10100DATA 0.000000,0.000000
  151.  
  152. Yielding the result:
  153.  
  154. LEAST SQUARE LINEAR FUNCTION FIT
  155.  Y = A1*(X(1)) + A2*(X(1)^2) + A3*(X(1)^3)
  156.  
  157. A1 = 0.199997935
  158. A2 = 6.92216755E-6
  159. A3 = -0.200004818
  160.  
  161. RMS error = 1.8138212E-7
  162. MAX error = 3.0513911E-7
  163.                     
  164. Note that you must use the transformed value of x in the curve-fit equation,
  165. in this case
  166.  
  167. Y = .2*SQR(X) + 6.9E-6*(SQR(X))^2 - .2*(SQR(X))^3
  168.  
  169.   = .2*SQR(X)*(1-X) (the equation we used to generate the data)
  170.  
  171. As a comparison, you may like to remove the transformation and try this
  172. case again. You should find the RMS error is about 1000 times bigger.
  173. Transformation changes the shape of the graph, so, for example, if you
  174. were doing an exponential curve-fit and you used a transformation of
  175. LN(X), you would expect to see a straight line graph.
  176.  
  177. Epilog
  178. ------
  179. We've used this program quite a lot at work (a technical engineering
  180. company) and it does work well. Sorry about the graphs! Version 4 is in
  181. the pipeline, with various extra features, including:
  182.  
  183.        Improved graphics
  184.        Variable weighting on the input data
  185.        Data validation
  186.        Fewer bugs(!)
  187.  
  188. This manual was written in about 2 hours, and probably looks like it. If
  189. you find LSLinF useful, or have any further queries, you can get in touch
  190. with us:
  191.  
  192. AUTHOR:                            COLLABORATOR:
  193.  
  194. Alan Linton                        Lorcan Mongey
  195. 16 Victoria Road                   56 Salisbury Court
  196. Belfast BT4 1QU                    Belfast BT7 1DD
  197.                                    
  198.                                    BBS: (id = lorcan)
  199.                                    The World of Cryton (0749 670030)
  200.                                    CIX                 (081 390 1244)
  201.                                    Arcade              (081 654 2212)
  202.